﻿<Window x:Class="MiniTwitter.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MiniTwitter="clr-namespace:MiniTwitter"
    xmlns:MiniTwitter_Controls="clr-namespace:MiniTwitter.Controls"
    xmlns:MiniTwitter_Properties="clr-namespace:MiniTwitter.Properties" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:Input="clr-namespace:MiniTwitter.Models.Input" xmlns:Twitter="clr-namespace:MiniTwitter.Models.Net.Twitter" x:Name="Window"
    Title="MiniTwitter" Width="350" Height="450" MinWidth="150" MinHeight="100" ResizeMode="CanResizeWithGrip"
    Style="{DynamicResource MainWindowStyle}" FontFamily="{Binding Path=FontName, Source={x:Static MiniTwitter_Properties:Settings.Default}, FallbackValue=Meiryo}" Icon="Resources\MiniTwitter_small.ico"
    Loaded="MainWindowLoaded" Initialized="MainWindowInitialized" Activated="MainWindowActivated" Closing="MainWindowClosing" Closed="MainWindow_Closed"
    TextOptions.TextFormattingMode="Display" UseLayoutRounding="True">
    <Window.TaskbarItemInfo>
        <TaskbarItemInfo Description="MiniTwitter">
            <TaskbarItemInfo.ThumbButtonInfos>
                <ThumbButtonInfo DismissWhenClicked="False" Command="Input:Commands.Refresh"
                                 CommandTarget="{Binding ElementName=Window}" Description="更新"
                                 ImageSource="/MiniTwitter;component/Resources/arrow_refresh.png" />
            </TaskbarItemInfo.ThumbButtonInfos>
        </TaskbarItemInfo>
    </Window.TaskbarItemInfo>
    <Window.CommandBindings>
        <CommandBinding Command="Input:Commands.Update" Executed="UpdateCommandExecuted"/>
        <CommandBinding Command="Input:Commands.Refresh" Executed="RefreshCommandExecuted"/>
        <CommandBinding Command="Input:Commands.Reply" Executed="ReplyCommandExecuted"/>
        <CommandBinding Command="Input:Commands.ReTweet" Executed="ReTweetCommandExecuted"/>
        <CommandBinding Command="Input:Commands.ReTweetApi" Executed="ReTweetApiCommand_Executed" />
        <CommandBinding Command="Input:Commands.ReplyMessage" Executed="ReplyMessageCommand_Executed"/>
        <CommandBinding Command="Input:Commands.Delete" Executed="DeleteCommand_Executed"/>
        <CommandBinding Command="Input:Commands.Favorite" Executed="FavoriteCommand_Executed"/>
        <CommandBinding Command="Input:Commands.TimelineStyle" Executed="TimelineStyleCommand_Executed"/>
        <CommandBinding Command="Input:Commands.MoveToUserPage" Executed="MoveToUserPageCommand_Executed"/>
        <CommandBinding Command="Input:Commands.MoveToStatusPage" Executed="MoveToStatusPageCommand_Executed"/>
        <CommandBinding Command="Input:Commands.MoveToReplyPage" Executed="MoveToReplyPageCommand_Executed" />
        <CommandBinding Command="Input:Commands.ReadAll" Executed="ReadAllCommand_Executed"/>
        <CommandBinding Command="Input:Commands.ScrollUp" Executed="ScrollUpCommand_Executed" />
        <CommandBinding Command="Input:Commands.ScrollDown" Executed="ScrollDownCommand_Executed" />
        <CommandBinding Command="Input:Commands.Copy" Executed="CopyCommand_Executed"/>
        <CommandBinding Command="Input:Commands.CopyUrl" Executed="CopyUrlCommand_Executed"/>
        <CommandBinding Command="Input:Commands.SortCategory" Executed="SortCategoryCommand_Executed"/>
        <CommandBinding Command="Input:Commands.SortDirection" Executed="SortDirectionCommand_Executed"/>
        <CommandBinding Command="Input:Commands.AddTimeline" Executed="AddTimelineCommand_Executed"/>
        <CommandBinding Command="Input:Commands.EditTimeline" Executed="EditTimelineCommand_Executed"/>
        <CommandBinding Command="Input:Commands.DeleteTimeline" Executed="DeleteTimelineCommand_Executed"/>
        <CommandBinding Command="Input:Commands.ClearTimeline" Executed="ClearTimelineCommand_Executed"/>
        <CommandBinding Command="Input:Commands.Apportion" Executed="ApportionCommand_Executed" />
        <CommandBinding Command="Input:Commands.Footer" Executed="FooterCommand_Executed" />
        <CommandBinding Command="Input:Commands.Search" Executed="SearchCommand_Executed" />
        <CommandBinding Command="Input:Commands.Twitpic" Executed="TwitpicCommand_Executed" />
        <CommandBinding Command="Input:Commands.PlayTitle" Executed="PlayTitleCommand_Executed" CanExecute="PlayTitleCommand_CanExecuted" />
        <CommandBinding Command="Input:Commands.InReplyTo" Executed="InReplyToCommand_Executed" />
        <CommandBinding Command="Input:Commands.GpsLocation" Executed="GpsLocationCommand_Executed" CanExecute="GpsLocationCommand_CanExecute" />
        <CommandBinding Command="Input:Commands.Follow" Executed="FollowCommand_Executed" />
        <CommandBinding Command="Input:Commands.Unfollow" Executed="UnfollowCommand_Executed" />
        <CommandBinding Command="Input:Commands.Block" Executed="BlockCommand_Executed" />
        <CommandBinding Command="Input:Commands.Hashtag" Executed="HashtagCommand_Executed" />
    </Window.CommandBindings>
    <Window.Resources>
        <ContextMenu x:Key="notifyMenu">
            <MenuItem Header="サイレントモード(_Q)" IsCheckable="True" Checked="SilentMenuItem_Checked" Unchecked="SilentMenuItem_Unchecked" />
            <Separator />
            <MenuItem Header="終了(_X)" Click="CloseMenuItem_Click" />
        </ContextMenu>
        <MiniTwitter:InvertBooleanConverter x:Key="InvertBooleanConverter" />
    </Window.Resources>
    <DockPanel>
        <Grid DockPanel.Dock="Top" Margin="6">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="What's happening?" VerticalAlignment="Center" Style="{DynamicResource HeaderTextBlockStyle}"/>
            <TextBlock Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center"
                       Text="{Binding ElementName=TweetTextBox, Path=Text.Length, Converter={StaticResource TextCountConverter}}"
                       Style="{DynamicResource CounterTextBlockStyle}"/>
            <TextBlock Grid.Row="1" Text="{Binding Path=TweetFooter, Source={x:Static MiniTwitter_Properties:Settings.Default}}"
                       VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,5,8" Foreground="#FFA0A0A0"
                       Visibility="{Binding Path=EnableTweetFooter, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Static MiniTwitter_Properties:Settings.Default}}" />
            <TextBox x:Name="TweetTextBox" Grid.Row="1" Margin="0,4,0,6" AcceptsTab="True" TextWrapping="Wrap" TextChanged="TweetTextBoxTextChanged" KeyUp="TweetTextBoxKeyUp"
                     VerticalScrollBarVisibility="Auto" KeyDown="TweetTextBoxKeyDown" PreviewKeyDown="TweetTextBoxPreviewKeyDown"  Style="{DynamicResource TweetTextBoxStyle}"
                     Background="Transparent">
                <TextBox.CommandBindings>
                    <CommandBinding Command="Paste" Executed="PasteCommand_Executed" />
                </TextBox.CommandBindings>
                <TextBox.Text>
                    <Binding Path="TargetValue" ElementName="Window" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <MiniTwitter_Controls:TweetValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <Popup x:Name="popup" PopupAnimation="None" StaysOpen="False" Focusable="False" MaxHeight="250" MinWidth="180">
                <ListBox x:Name="usersListBox" Focusable="False" Background="White" Foreground="Black" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                         MouseUp="UsersListBoxMouseUp" MouseDoubleClick="UsersListBoxMouseDoubleClick">
                    <ListBox.Resources>
                        <DataTemplate DataType="{x:Type Twitter:User}">
                            <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                                <Image Source="{Binding Path=ImageUrl}" Width="18" Height="18" Margin="0,0,2,0" />
                                <TextBlock Text="@" VerticalAlignment="Center" />
                                <TextBlock Text="{Binding Path=ScreenName}" VerticalAlignment="Center" />
                            </StackPanel>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type s:String}">
                            <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                                <TextBlock Text="#" VerticalAlignment="Center" />
                                <TextBlock Text="{Binding}" VerticalAlignment="Center" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.Resources>
                </ListBox>
            </Popup>
            <Button x:Name="UpdateButton" Grid.Row="2" Content="update" Command="Input:Commands.Update"
                    CommandParameter="{Binding ElementName=TweetTextBox, Path=Text}" Style="{DynamicResource UpdateButtonStyle}"/>
            <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                <Button Margin="0,0,3,0" Click="HomeButtonClick" Style="{DynamicResource ImageButtonStyle}">
                    <Image Style="{DynamicResource HomeImageStyle}"/>
                </Button>
                <ToggleButton x:Name="searchButton" Margin="0,0,3,0" Style="{DynamicResource ImageToggleButtonStyle}" Checked="SearchButton_Checked" Unchecked="SearchButton_Unchecked">
                    <Image Style="{DynamicResource SearchImageStyle}"/>
                </ToggleButton>
                <Button Margin="0,0,3,0" Command="Input:Commands.Refresh"
                        Style="{DynamicResource ImageButtonStyle}">
                    <Image Style="{DynamicResource ArrowRefreshImageStyle}"/>
                </Button>
                <Button Click="SettingButtonClick" Style="{DynamicResource ImageButtonStyle}">
                    <Image Style="{DynamicResource SettingImageStyle}"/>
                </Button>
            </StackPanel>
            <Grid Grid.Row="3" Margin="0,6,0,0" Visibility="{Binding Path=IsChecked, ElementName=searchButton, Converter={StaticResource BooleanToVisibilityConverter}}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="検索:" Grid.Column="0" FontWeight="Bold" Margin="0,0,3,0" VerticalAlignment="Center" />
                <TextBox x:Name="searchTermTextBox" Grid.Column="1" HorizontalAlignment="Stretch" KeyUp="SearchTermTextBox_KeyUp" />
                <Button Grid.Column="2" Style="{DynamicResource ImageButtonStyle}" Click="SearchCancelButton_Click">
                    <Image Style="{DynamicResource CrossImageStyle}" />
                </Button>
            </Grid>
        </Grid>
        <StatusBar DockPanel.Dock="Bottom" Padding="0,0,15,0" VerticalContentAlignment="Center">
            <StatusBarItem>
                <TextBlock Text="{Binding ElementName=Window, Path=StatusText}"/>
            </StatusBarItem>
            <StatusBarItem HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal">
                    <Image Width="14" Height="14" Margin="0,0,5,0" Style="{DynamicResource PopupImageStyle}"/>
                    <TextBlock Text="{Binding ElementName=TimelineTabControl, Path=SelectedContent.Items.Count, StringFormat=\{0\} 件}"/>
                </StackPanel>
            </StatusBarItem>
        </StatusBar>
        <TabControl x:Name="TimelineTabControl" DockPanel.Dock="Top" SelectedIndex="0"
                    ItemsSource="{Binding ElementName=Window, Path=Timelines}" Style="{DynamicResource TimelineTabControlStyle}"
                    ItemContainerStyle="{DynamicResource TimelineTabItemStyle}" IsSynchronizedWithCurrentItem="True" SelectionChanged="TimelineTabControl_SelectionChanged">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" />
                        <Border CornerRadius="4" Background="DarkGray" Margin="5,0,0,0" VerticalAlignment="Center">
                            <Border.Style>
                                <Style TargetType="{x:Type Border}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=UnreadCount}" Value="0">
                                            <Setter Property="Visibility" Value="Collapsed" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Border.Style>
                            <TextBlock Text="{Binding Path=UnreadCount}" Foreground="White" Margin="2,0,2,0"  />
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ListBox x:Name="TimelineListBox" IsSynchronizedWithCurrentItem="True" AlternationCount="2"
                             VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"
                             ScrollViewer.IsDeferredScrollingEnabled="True" ScrollViewer.ScrollChanged="TimelineListBox_ScrollChanged"
                             SelectionChanged="TimelineListBoxSelectionChanged" PreviewMouseWheel="TimelineListBox_MouseWheel"
                             MouseDoubleClick="TimelineListBoxMouseDoubleClick" ItemsSource="{Binding Path=Items}"
                             Style="{DynamicResource TimelineListBoxStyle}" ItemContainerStyle="{DynamicResource TimelineListBoxItemStyle}"
                             ItemTemplateSelector="{StaticResource TimelineStyleSelector}" HorizontalContentAlignment="Stretch"/>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </DockPanel>
</Window>
